/**
 * 发表新帖子
 * @author Qasim <15750783791@163.com>
 * @version 2016/9/19
 */
var self = pic = '';

var index = 1;

var files = [];

document.addEventListener('plusready' , ready ,  false);

function ready(){
	
	self = plus.webview.currentWebview(); 
	
	var uid = self.uid;
	
	var id = self.aid;
	
	//加载数据
	$.ajax({
		type:"post",
		url:apiRoot +'/Home/Demand/loadDemandById/',
		data : {
			id :id
		},
		dataType : 'json',
		success : function(data){
			plus.nativeUI.closeWaiting();
			if(data){
				$('.field').each(function(index){
					switch (index){
						case 0:
							$(this).val(data.title); 
							break;
						case 1:
							$(this).val(data.demand);
							break;
						case 2:
							$(this).val(data.contacts);
							break;
						case 3:
							$(this).val(data.phone);
							break;
						case 4:
							$(this).val(data.email);
							break;
						case 5:
							$(this).val(data.company);
							break;
					}
				})
			} 
		}, 
		error : function(e){
			
			console.log(JSON.stringify(e));
			
			plus.nativeUI.closeWaiting();
			
			toast('连接超时,请检查网络');
			
		}
	})
	//提交反馈意见
	$('.modify_demand').on('click' , function(){ 
		var title = demand = contacts = phone = email = company = '';
		$('.field').each(function(index){
			switch (index){
				case 0:
					title = $(this).val(); 
					break;
				case 1:
					demand = $(this).val();
					break;
				case 2:
					contacts = $(this).val();
					break;
				case 3:
					phone = $(this).val();
					break;
				case 4:
					email = $(this).val();
					break;
				case 5:
					company = $(this).val();
					break;
			}
		})
		plus.nativeUI.showWaiting('修改中...');
		$.ajax({
			type:"post",
			url:apiRoot +'/Home/Demand/modify_demand/',
			data : {
				id : id,
				uid : self.uid ,
				tid : 38,
				title : title ,
				demand : demand,
				contacts : contacts,
				phone :phone,
				email : email,
				company :company
			},
			success : function(data){
				plus.nativeUI.closeWaiting();
				
				if(data == 1){
					toast('修改成功');
					
					mui.fire(plus.webview.getWebviewById(self.page) , 'update_demand' ,{
	 					demand : demand
					});
					
					mui.fire(plus.webview.getWebviewById(self.top_page) , 'update_demand' ,{
	 					id : data  ,
	 					title : title
					});
					
					self.close();
				}else{
					toast('修改失败');
				}
			},
			error : function(e){
				
				console.log(JSON.stringify(e));
				
				plus.nativeUI.closeWaiting();
				
				toast('连接超时,请检查网络');
				
			}
		});
	})
	
}

// 添加照片
//function appendPic(p) {  
//	var newPath = p.replace(/\./g , new Date().getTime()+'.');
//	plus.zip.compressImage({
//			src:p,
//			dst:newPath,
//			quality:20
//		},
//		function() {
//			files.push({name:"uploadkey"+index,path:p});
//			index++;
//			upload();
//		},function(error) {
//			toast('压缩图片失败');
//	});
//}
//
//function upload(){
//	plus.nativeUI.showWaiting('图片处理中...');
//	if(files.length <= 0){  
//		toast("请上传头像！");
//		return;
//	}
//	var server = apiRoot+'/Home/Member/uploadPic/'; 
//	var task = plus.uploader.createUpload(server,
//		{method:"POST"},
//		function(t,status){ //上传完成
//			plus.nativeUI.closeWaiting();
//			if(status == 200){
//				toast('处理完成');
//				$('.content').append('<img style="height:100px;width:100px" src="'+getImgUrl(t.responseText)+'"/>');
//			}else{
//				plus.nativeUI.alert("上传失败："+status);
//			}
//		} 
//	);
//	for(var i = 0;i < files.length ; i++){
//		var f = files[i];
//		task.addFile(f.path,{key:f.name});
//	} 
//	task.start();
//}